package com.dome.mvp.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.dome.mvp.R;
import com.dome.mvp.utils.ScreenUtils;

/**
 * Time: 6/4/2021 11:46
 * Author: TAO
 * Description:
 */
public abstract class BaseFragment<V extends IBaseView, P extends BasePresenter<V>, VDB extends ViewDataBinding> extends Fragment implements IBaseView {

    protected VDB binding;
    protected P presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getLayoutRes() != 0) {
            if (isBinding()) {
                binding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false);
                return binding.getRoot();
            } else {
                return inflater.inflate(getLayoutRes(), container, false);
            }
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Toolbar toolbar = binding.getRoot().findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.post(() -> {
                ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
                layoutParams.height = ScreenUtils.getStatusBarHeight(getActivity());
                toolbar.setLayoutParams(layoutParams);
            });
        }


        // 创建实例对象
        presenter = createPresenter();
        if (presenter == null) return;
        // 添加视图层到逻辑处理
        presenter.attach((V) this);
        // presenter 添加生命周期监听
        getLifecycle().addObserver(presenter);
        // 注册第三方sdk
        registerSdk();
        // 初始化
        init();
    }

    protected abstract P createPresenter();

    @Override
    public void showError(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean isBinding() {
        return true;
    }

    @Override
    public boolean isTranslucent() {
        return true;
    }


    @Override
    public void init() {

    }

    @Override
    public void registerSdk() {

    }

    @Override
    public void unRegisterSdk() {

    }

    @Override
    public void onDestroy() {
        if (presenter != null) {
            getLifecycle().removeObserver(presenter);
            presenter.detach();
            presenter = null;
        }
        if (binding != null) {
            binding.unbind();
            binding = null;
        }
        unRegisterSdk();
        super.onDestroy();
    }
}
